/**
 * 
 */
package com.armail.client.view.javafx;

import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;

import com.armail.client.dto.Mail;

/**
 * @author arpit
 *
 */
public class FXMailDetailsPane extends VBox  {

	private final TextArea bodyTextArea = new TextArea();
	private final TextField toTextField = new TextField();
	private final TextField subjectTextFieldField = new TextField();
	
	private Mail model;
	
	public FXMailDetailsPane() {
		setSpacing(15);
		setPrefWidth(200);

		toTextField.setMinHeight(25);
		toTextField.setPromptText("To");

		subjectTextFieldField.setMinHeight(25);
		subjectTextFieldField.setPromptText("Subject");

		bodyTextArea.setPromptText("Body");
		getChildren().addAll(toTextField, subjectTextFieldField, bodyTextArea);
		VBox.setVgrow(bodyTextArea, Priority.ALWAYS);
	}
	
	public void setModel(Mail model) {
		if(this.model != null) {
			unbindProperties();
		}
		this.model = model;
		bindProperties();
	}
	
	private void unbindProperties() {
		bodyTextArea.textProperty().unbindBidirectional(model.bodyProperty());
		subjectTextFieldField.textProperty().unbindBidirectional(model.subjectProperty());
		toTextField.textProperty().unbindBidirectional(model.toUserProperty());
	}

	private void bindProperties() {
		bodyTextArea.textProperty().bindBidirectional(model.bodyProperty());
		subjectTextFieldField.textProperty().bindBidirectional(model.subjectProperty());
		toTextField.textProperty().bindBidirectional(model.toUserProperty());
	}

	public TextArea getBodyTextArea() {
		return bodyTextArea;
	}
}
